{% set pypi_name = 'networking-vsphere' %}
{% set source = fetch_source('https://tarballs.openstack.org/networking-vsphere/networking-vsphere-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        OVSvApp and DVS VMware vSphere drivers for Neutron
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://docs.openstack.org/{{ pypi_name }}
Source0:        {{ source|basename }}
Source1:        openstack-neutron-dvs-agent.service
Source2:        openstack-neutron-ovsvapp-agent.service
Source3:        openstack-neutron-ovsvapp-agent-monitor.service
# https://review.opendev.org/#/c/671155/
Patch1:         0001-Switch-documentation-build-to-python3.patch
BuildRequires:  fdupes
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('Babel') }}
BuildRequires:  {{ py3('devel') }}
BuildRequires:  {{ py3('kombu') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  python3-nova
BuildRequires:  {{ py3('oslo.vmware') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('ovsdbapp') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('testrepository') }}
BuildRequires:  {{ py3('testresources') }}
BuildRequires:  {{ py3('testscenarios') }}
BuildRequires:  {{ py3('testtools') }}
Requires:       python3-{{ pypi_name }} = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
%{?systemd_requires}
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun): systemd
Requires(preun): systemd
%endif

%description
Neutron ML2 drivers and L2 agents for OVSvApp - a Neutron supported
solution for vSphere deployments in the form of a service VM called
OVSvApp VM which steers the ESX tenant VMs' traffic through it.

%package -n python3-{{ pypi_name }}
Summary:        OVSvApp and DVS Drivers - Python libraries
Requires:       {{ py3('Babel') }}
# NOTE(dmllr): Todo, need to unwind recursive dependencies
#Requires:       python3-neutron
Requires:       python3-nova
Requires:       {{ py3('oslo.vmware') }}
Requires:       {{ py3('ovsdbapp') }}

%description -n python3-{{ pypi_name }}
The OVSvApp solution comprises of a service VM called OVSvApp VM hosted on each
ESXi hypervisor within a cluster and two vSphere Distributed Switches (VDS).
Two ML2 mechanism drivers are provided for Neutron - one for OVSvApp and
another one for DVS.

This package contains the Python libraries.

%package dvs-agent
Summary:        DVS L2 agent for %{name} plugin
Group:          System/Management
Requires:       %{name} = %{version}-%{release}

%description dvs-agent
Contains the DVS L2 agent service for %{name}

%package ovsvapp-agent
Summary:        OVSvApp agents for %{name} plugin
Group:          System/Management
Requires:       %{name} = %{version}-%{release}
Requires:       openvswitch

%description ovsvapp-agent
Contains the OVSvApp agent services for %{name}

%package doc
Summary:        Documentation for %{name}
Group:          Documentation/HTML
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('oslosphinx') }}

%description doc
This package contains extra documentation for %{name}.

%package test
Summary:        Testsuite for the OVSvApp VMware
Group:          System/Management
Requires:       %{name} = %{version}-%{release}
Requires:       {{ py3('kombu') }}
Requires:       {{ py3('mock') }}
Requires:       {{ py3('oslotest') }}
Requires:       {{ py3('testrepository') }}
Requires:       {{ py3('testresources') }}
Requires:       {{ py3('testscenarios') }}
Requires:       {{ py3('testtools') }}

%description test
The testsuite for the VMware vSphere ML2 drivers for OpenStack Neutron

%prep
%autosetup -p1 -n {{ pypi_name }}-{{upstream_version}}
%py_req_cleanup

%build
%{py3_build}
PYTHONPATH=. PBR_VERSION={{ upstream_version }} %sphinx_build -b html doc/source doc/build/html
# remove the sphinx-build leftovers
rm -rf doc/build/html/.{doctrees,buildinfo}

%install
%{py3_install}
mv %{buildroot}%{_prefix}%{_sysconfdir} %{buildroot}%{_sysconfdir}
install -p -D -m 644 %{SOURCE1} %{buildroot}%{_unitdir}/openstack-neutron-dvs-agent.service
install -p -D -m 644 %{SOURCE2} %{buildroot}%{_unitdir}/openstack-neutron-ovsvapp-agent.service
install -p -D -m 644 %{SOURCE3} %{buildroot}%{_unitdir}/openstack-neutron-ovsvapp-agent-monitor.service
chmod +x %{buildroot}%{python3_sitelib}/networking_vsphere/monitor/ovsvapp-agent-monitor.sh
# Copy the drivers provided from python-networking-vsphere for
# Nova. Apparently VMWare drivers are out of the Nova tree, and
# maintained outside Nova, so we need to copy the provided driver.
mkdir -p %{buildroot}%{python3_sitelib}/nova/virt/vmwareapi/
cp -a %{buildroot}%{python3_sitelib}/networking_vsphere/nova/virt/vmwareapi/ovsvapp_vc_driver.py* \
      %{buildroot}%{python3_sitelib}/nova/virt/vmwareapi/
cp -a %{buildroot}%{python3_sitelib}/networking_vsphere/nova/virt/vmwareapi/ovsvapp_vmops.py* \
      %{buildroot}%{python3_sitelib}/nova/virt/vmwareapi/
%fdupes %{buildroot}%{python3_sitelib}

# %check
# python2 setup.py testr

%post dvs-agent
%systemd_post openstack-neutron-dvs-agent.service

%preun dvs-agent
%systemd_preun openstack-neutron-dvs-agent.service

%postun dvs-agent
%systemd_postun_with_restart openstack-neutron-dvs-agent.service

%post ovsvapp-agent
%systemd_post openstack-neutron-ovsvapp-agent.service
%systemd_post openstack-neutron-ovsvapp-agent-monitor.service

%preun ovsvapp-agent
%systemd_preun openstack-neutron-ovsvapp-agent-monitor.service
%systemd_preun openstack-neutron-ovsvapp-agent.service

%postun ovsvapp-agent
%systemd_postun_with_restart openstack-neutron-ovsvapp-agent-monitor.service
%systemd_postun_with_restart openstack-neutron-ovsvapp-agent.service

%files
%dir %{_sysconfdir}/neutron
%dir %{_sysconfdir}/neutron/plugins
%dir %{_sysconfdir}/neutron/plugins/ml2
%config(noreplace) %{_sysconfdir}/neutron/ml2_conf_vmware_dvs.ini
%config(noreplace) %{_sysconfdir}/neutron/plugins/ml2/ovsvapp_agent.ini
# Patch no longer required in nova stable/newton
%exclude %{_prefix}%{_localstatedir}/tmp/nova.patch
%{_bindir}/ovsvapp-manage-dvpg
%{_bindir}/ovsvapp-manage-dvs

%files -n python3-{{ pypi_name }}
%doc README.rst
%license LICENSE
%{python3_sitelib}/networking_vsphere
%{python3_sitelib}/networking_vsphere-*.egg-info
%{python3_sitelib}/nova/virt/vmwareapi/*
# Part of test subpackage
%exclude %{python3_sitelib}/networking_vsphere/tests

%files dvs-agent
%{_unitdir}/openstack-neutron-dvs-agent.service
%{_bindir}/neutron-dvs-agent

%files ovsvapp-agent
%{_unitdir}/openstack-neutron-ovsvapp-agent.service
%{_unitdir}/openstack-neutron-ovsvapp-agent-monitor.service
%{_bindir}/neutron-ovsvapp-agent
%{_bindir}/neutron-ovsvapp-agent-monitor
%{_bindir}/neutron-ovsvapp-db-manage

%files doc
%license LICENSE
%doc doc/build/html

%files test
%{python3_sitelib}/networking_vsphere/tests

%changelog

